package org.dk.poitalk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import org.dk.poitalk.exception.DuplicateEntityException;
import org.dk.poitalk.model.EntityClient;
import org.dk.poitalk.model.TipoEntityClient;
import org.dk.poitalk.service.entity.EntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.support.PropertiesLoaderSupport;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.LocaleResolver;

import com.google.apphosting.api.DatastorePb.GetRequest;
import com.jappstart.model.auth.UserAccount;
import com.jappstart.service.auth.EnhancedUserDetailsService;
import com.jappstart.service.cache.CacheService;

@Controller
@RequestMapping("/feedback")
@SessionAttributes(FeedbackController.FEEDBACK)
public class FeedbackController {
	private static final Logger logger = Logger.getLogger(FeedbackController.class
			.getName());


	protected static final String FEEDBACK = "feedback";
	
	private EntityService entityService;

	private LocaleResolver localeResolver;
	
	private EnhancedUserDetailsService userDetailsService;

	private MessageSource messageSource;
	
	public EntityService getEntityService() {
		return entityService;
	}

	@Autowired
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * Gets the locale resolver.
	 * 
	 * @return the locale resolver
	 */
	public final LocaleResolver getLocaleResolver() {
		return localeResolver;
	}

	/**
	 * Sets the locale resolver.
	 * 
	 * @param localeResolver
	 *            the locale resolver
	 */
	@Autowired
	public final void setLocaleResolver(final LocaleResolver localeResolver) {
		this.localeResolver = localeResolver;
	}
	
	/**
	 * Gets the user details service.
	 * 
	 * @return the user details service
	 */
	public final EnhancedUserDetailsService getUserDetailsService() {
		return userDetailsService;
	}

	/**
	 * Sets the user details service.
	 * 
	 * @param userDetailsService
	 *            the user details service
	 */
	@Autowired
	public final void setUserDetailsService(
			final EnhancedUserDetailsService userDetailsService) {
		this.userDetailsService = userDetailsService;
	}

	

	public MessageSource getMessageSource() {
		return messageSource;
	}
	
	@Autowired
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	@RequestMapping(value = "/claim", method = RequestMethod.GET)
    public final String create() {
        return "claim";
    }	

	/** TODO Terminar el formulario de encuesta*/
	/** TODO Guardar las encuestas relacionadas a una entidad y a un usuario*/
    /**
     * Determines if an account with the given username exists or not.
     *
     * @param request the request
     * @return the response
     */
    @RequestMapping(value = "/feedbacking", method = RequestMethod.GET)
    public final String feedbacking(
    	final HttpServletRequest request) {
		String username = ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        EntityClient entityClientIN = new EntityClient(request.getParameter("idplace").toString(),TipoEntityClient.valueOf(request.getParameter("tipoentityclient").toString()),null);
        
        logger.info(String.format("Estoy feedback la siguiente Entidad: %s", entityClientIN.toString()));

        return "feedback";
    } 

    /** TODO Necesita ser una página especial que te saque los datos del usuario*/
    /**
     * Determines if an account with the given username exists or not.
     *
     * @param request the request
     * @return the response
     */
    @RequestMapping(value = "/feedbacker", method = RequestMethod.GET)
    @ResponseBody
    public final String feedbacker(
        	final HttpServletRequest request) {
		String username = ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        logger.info(String.format("EL QRCode es del feedbaker %s y yo estoy logado como %s", request.getParameter("username").toString(),username));

        return "feedback";
    }
     
}
